package com.czy.owner.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.OnLineOrderServiceModel;

import java.util.List;


/**
 * Created by wangyang
 */
public class OrderServiceAdapter extends BaseRecyclerAdapter<OnLineOrderServiceModel.DataBean, OrderServiceAdapter.ItemServiceHolder> {
    private Context ctx;

    public OrderServiceAdapter(Context context, List<OnLineOrderServiceModel.DataBean> data) {
        super(context, data);
        this.ctx = context;
    }

    @Override
    public void onBindViewHolder(ItemServiceHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));

    }

    @Override
    public ItemServiceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemServiceHolder(layoutInflater.inflate(R.layout.item_order_service, parent, false));
    }

    public class ItemServiceHolder extends BaseRecyclerAdapter.ViewHolder {
        private RadioButton rbItemServiceName;

        public ItemServiceHolder(View itemView) {
            super(itemView);
            rbItemServiceName = (RadioButton) itemView.findViewById(R.id.rb_item_service_name);
        }

        public void bindData(final OnLineOrderServiceModel.DataBean model) {
            rbItemServiceName.setText(model.getServiceName());
            rbItemServiceName.setChecked(model.isState());
                if (model.getSubs().size() >= 1) {
                    for (int i = 0; i < model.getSubs().size(); i++) {
                        if (model.getSubs().get(i).isState()) {
                            rbItemServiceName.setChecked(true);
                            break;
                        }
                    }
                }


            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (model.getSubs().size() >= 1) {
                        showTypeDialog(model, model.getServiceName());
                    } else {
                        if (data.get(position).isState()) {
                            data.get(position).setState(false);
                            rbItemServiceName.setChecked(false);
                        } else {
                            data.get(position).setState(true);
                            rbItemServiceName.setChecked(true);
                        }
                    }

//                    notifyItemRangeChanged(0, data.size());

                }
            };


            itemView.setOnClickListener(clickListener);
            rbItemServiceName.setOnClickListener(clickListener);
        }
    }

    public void myNotifyDataSetChanged(){
        notifyDataSetChanged();
    }

    public Dialog mTypeDialog;
    private OrderServiceSubAdapter subAdapter;

    private void showTypeDialog(OnLineOrderServiceModel.DataBean model, String serviceName) {
        WindowManager windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = windowManager.getDefaultDisplay();
        mTypeDialog = new Dialog(ctx, R.style.AlertDialogStyle);
        View dialogView = LayoutInflater.from(ctx).inflate(R.layout.dialog_order_service_sub, null);
        TextView tvServiceSub = (TextView) dialogView.findViewById(R.id.tv_service_sub);
        tvServiceSub.setText("选择" + serviceName + "分类");
        Button btnSure = (Button) dialogView.findViewById(R.id.btn_sure);
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTypeDialog.dismiss();
                notifyDataSetChanged();
            }
        });

        RecyclerView rvType = (RecyclerView) dialogView.findViewById(R.id.rv_archive_add_type);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(ctx, 3);
        rvType.setLayoutManager(layoutManager);
        subAdapter = new OrderServiceSubAdapter(ctx, model.getSubs());
        rvType.setAdapter(subAdapter);
        mTypeDialog.setContentView(dialogView);
        mTypeDialog.setCancelable(true);
        mTypeDialog.setCanceledOnTouchOutside(false);

        Window dialogWindow = mTypeDialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mDisplay.getWidth() * 0.86);
        dialogWindow.setAttributes(p);
        mTypeDialog.show();

        mTypeDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
        {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
            {
                if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
                {
                    dialog.dismiss();
                }
                return false;
            }
        });

    }


}
